package cate.game.role.res.findback.po;

import cate.common.table.d.GDFindBack;
import cate.common.table.findback.base.FindBackBaseRow;
import cate.common.util.TimeTool;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@NoteClass("角色找回单功能")
public class RoleFindBackFuncPO extends RoleDependent {
    @NoteField("上次触发检测的时间")
    private long lastCheckTime;
    @NoteField("找回次数")
    public int times;
    @NoteField("当前找回次数")
    public int nowTimes;
    @NoteField("资源")
    public MixRes payReward;
    @NoteField("免费找回奖励")
    public MixRes freeReward;
    @NoteField("消耗")
    public MixRes cost;

    public int getFuncId() {
        return 0;
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (lastCheckTime == 0) {
            lastCheckTime = TimeTool.getZeroTimeStamp(System.currentTimeMillis());
        }
    }

    @JsonIgnore
    protected FindBackBaseRow getConfig() {
        return role.getGame().table.findBack.base.get(getFuncId());
    }

    public void clear() {
        times = 0;
        nowTimes = 0;
        payReward = null;
        freeReward = null;
        cost = null;
    }

    /**
     * 触发计算
     */
    public final void trigger() {
        clear();
        boolean skipDay = TimeTool.getDistanceDays(lastCheckTime, System.currentTimeMillis()) > 1;
        calcFindBack(skipDay);
        lastCheckTime = TimeTool.getZeroTimeStamp(System.currentTimeMillis());
    }

    /**
     * GM触发
     */
    public void GMTrigger(boolean skipDay) {
        clear();
        calcFindBack(skipDay);
        lastCheckTime = TimeTool.getZeroTimeStamp(System.currentTimeMillis());
    }

    /**
     * 计算找回
     *
     * @param skipDay 是否跳天
     */
    public void calcFindBack(boolean skipDay) {

    }

    /**
     * 领取找回资源
     *
     * @param type 类型
     * @return 领取结果
     */
    public EcResult<Void> onGetFindBackRes(byte type) {
        EcResult<Void> r = new EcResult<>();
        if (type != GDFindBack.FIND_BACK_TYPE_FREE && type != GDFindBack.FIND_BACK_TYPE_M1) {
            return r.fail("不存在的找回类型");
        }
        MixRes realCost = type == GDFindBack.FIND_BACK_TYPE_FREE ? null : this.cost;
        if (freeReward == null || payReward == null) {
            return r.fail("没有奖励可以找回");
        }
        if (nowTimes >= times) {
            return r.fail("所有奖励已找回");
        }
        if (realCost != null) {
            EcResult<?> consumeRes = realCost.consume(role);
            if (!consumeRes.ok()) {
                return r.fail(consumeRes.message);
            }
        }
        nowTimes += 1;
        MixRes reward = type == GDFindBack.FIND_BACK_TYPE_FREE ? freeReward : payReward;
        new MixResAdder().setRes(reward)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        return r.success();
    }

    /**
     * 领取找回资源
     *
     * @param type 类型
     * @return 领取结果
     */
    public EcResult<MixRes> onGetAllFindBackRes(byte type) {
        EcResult<MixRes> r = new EcResult<>();
        if (freeReward == null || payReward == null) {
            return r.fail("没有奖励可以找回");
        }
        if (nowTimes >= times) {
            return r.fail("所有奖励已找回");
        }
        int remainTimes = times - nowTimes;
        nowTimes = times;
        r.data = (type == GDFindBack.FIND_BACK_TYPE_FREE ? freeReward : payReward).multiply(remainTimes);
        return r;
    }

    public MixRes remainTimesCost() {
        int remainTimes = times - nowTimes;
        if (remainTimes > 0) {
            MixRes realCost = this.cost;
            if (realCost != null) {
                return realCost.multiply(remainTimes);
            }
        }
        return null;
    }
}
